package com.tbm.console.proj.controller;

import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.*;
import com.tbm.console.proj.service.ReportConfigInfoService;
import com.tbm.console.proj.service.ReportService;
import com.tbm.console.proj.service.SystemMenuService;
import com.tbm.console.proj.service.SystemRoleService;
import com.tbm.console.util.JsonUtils;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.PageData;
import com.tbm.console.util.RightsHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/report")
@Slf4j
public class ReportDisplay extends BaseController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/{base}/{report}")
    public ModelAndView rollDayReport(@PathVariable("base") String base,@PathVariable("report") String report){
        ModelAndView mv = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            String host = "http://10.10.131.1";
            String port = "8082";
            String path = "WebReport/ReportServer";
            String firstParam = "reportlet";
            String url = host + ":" + port + "/" + path + "?" + firstParam + "=" + base + "/" + report + ".cpt";
            mv.addObject("report", url);
            viewName = "report/report-template" ;
            //统计报表点击率
            try{
                String key = report;
                String value = stringRedisTemplate.opsForValue().get(key);
                if(null == value){


                    stringRedisTemplate.opsForValue().set(key,"1");
                }else{
                    value = String.valueOf(Integer.valueOf(value) + 1);
                    stringRedisTemplate.opsForValue().set(key,value);
                }
                log.info(value);
            }catch (Exception e){

            }
        }
        mv.setViewName(viewName);

        return mv;
    }

//    @RequestMapping(value = "/report-2250")
//    public ModelAndView uploadSolution(){
//        PageData pd = new PageData();
//        pd.put("parentId",38);
//        pd.put("deleteFlag", InsUpdDelFlagConstant.LOCALLOGDELETEFLAG);
//        List<ReportEntity> infoList = reportService.findAll(pd);
//        ModelAndView mv = this.getModelAndView();
//        String viewName = "login";
//        if(JurisdictionUtil.isValid()){
//            viewName = "report/report-2250";
//            List<ReportConfigInfoEntity> all = reportConfigInfoService.findAll();
//            String host = "http://10.10.131.1";
//            String port = "8082";
//            String path = "WebReport/ReportServer";
//            String firstParam = "reportlet";
//            String url = host + ":" + port + "/" + path + "?" + firstParam + "=";
//            if(all.size()==1){
//                ReportConfigInfoEntity entity = all.get(0);
//                url = entity.getHost() + ":" + entity.getPort() + "/" + entity.getPath() + "?" + entity.getFirstParam()+ "=";
//            }
//            List<PageData> subReport = new ArrayList<>();
//            for(ReportEntity entity : infoList){
//                PageData pageData = new PageData();
//                String fineReport = url + entity.getFirstParam();
//                pageData.put("URL", fineReport);
//                pageData.put("Desc", entity.getDescriptionCN());
//                subReport.add(pageData);
//            }
//            mv.addObject("reportURL",subReport);
//        }
//        mv.setViewName(viewName);
//        return mv;
//    }
}
